# python -m project_root.api.api_userlist
from .api_token import Token 
from util.time_util import timestran
import requests


class Api_UserList(Token):


    def get_list(self,username='',real_name=''):
        url = 'http://hxzhan.rtcloud.com:32111/tm/roles/user/list'
        json = {
            'limit':10,
            'page':1
        }
        if username != '':
            json.update(username = username)
        if real_name != '':
            json.update(real_name = real_name)
        response = requests.post(url=url,json=json,headers=self.get_header())
        return_data = response.json()['data']['list']
        values = []
        keys = ['username','real_name','remark','ctl_role','data_role','created_at','status']
        for node in return_data:
            for key in keys:
                if key == 'ctl_role':
                    str_c = ''
                    for c_role in node[key]:
                        str_c+=c_role['name']
                    values.append(str_c)
                elif key == 'data_role':
                    str_d = ''
                    for d_role in node[key]:
                        str_d+=d_role['name']
                    values.append(str_d)
                elif key == 'created_at':
                    values.append(timestran(node[key]))
                elif key == 'status':
                    if node[key] == 1:
                        values.append('已启用')
                    elif node[key] == 2:
                        values.append('未启用')
                else:
                    values.append(node[key])
        return values
    
# api = Api_UserList()
# print(api.get_list(username='%'))